namespace ForumExplorer.Extension.MSNSearch
{
    using System;
    using ForumsExplorer.Extension.MSNSearch;

    public class Search
    {
        public Search()
        {
            this.queryString = "";
            this.terms = new SearchTerms();
        }

		/// <revisionHistory>
		/// <revision author="Peter Ritchie" date="13-jul=06">
		/// removed CA1709 warning
		/// </revision>
		/// </revisionHistory>
		public string GetRSSURL()
        {
            return (this.GetURL() + "&format=rss&FORM=RSRE");
        }

		/// <revisionHistory>
		/// <revision author="Peter Ritchie" date="13-jul=06">
		/// Added test of isQuestion field
		/// Added handling of isComment field
		/// Added Debug.Assert
		/// removed CA1820 warnings
		/// </revision>
        /// <revision author="Moayad Mardini [mardini]" date="14-August-2006">
        /// Added the ability to search with user ID, not only with user-name
        /// </revision>
        /// </revision>
        /// <revision author="Moayad Mardini [mardini]" date="15-August-2006">
        /// Encode the URL (this.queryString) before return it
        /// </revision>
		/// </revisionHistory>
		public string GetURL()
        {
			System.Diagnostics.Debug.Assert(!(this.terms.IsQuestion && this.terms.IsComment));
            this.queryString = this.queryString + this.terms.FreeText;
            this.queryString = this.queryString + " site:forums.microsoft.com";
            if (!String.IsNullOrEmpty(this.terms.UserNameAnswerer))
            {
                if (this.terms.isIDAnswerer)
                {
                    this.queryString = this.queryString + " meta:Search.MSForums.ThreadAnswererUserID(\"" + SearchTerms.CleanString(this.terms.UserNameAnswerer) + "\")";
                }
                else
                {
                    this.queryString = this.queryString + " meta:Search.MSForums.ThreadAnswererUserName(\"" + SearchTerms.CleanString(this.terms.UserNameAnswerer) + "\")";
                }
            }
            if (!String.IsNullOrEmpty(this.terms.UserNameAsker))
            {
                if (this.terms.isIDAsker)
                {
                    this.queryString = this.queryString + " meta:Search.MSForums.ThreadStarterUserID(\"" + SearchTerms.CleanString(this.terms.UserNameAsker) + "\")";
                }
                else
                {
                    this.queryString = this.queryString + " meta:Search.MSForums.ThreadStarterUserName(\"" + SearchTerms.CleanString(this.terms.UserNameAsker) + "\")";
                }
            }
            if (!String.IsNullOrEmpty(this.terms.UserNameContributor))
            {
                if (this.terms.isIDContributor)
                {
                    this.queryString = this.queryString + " meta:Search.MSForums.ThreadContributorUserID(\"" + SearchTerms.CleanString(this.terms.UserNameContributor) + "\")";
                }
                else
                {
                    this.queryString = this.queryString + " meta:Search.MSForums.ThreadContributorUserName(\"" + SearchTerms.CleanString(this.terms.UserNameContributor) + "\")";
                }
            }
            if (this.terms.ForumIDs.Count > 0)
			{
				this.queryString += "(";
				int ordinal = 1;
				foreach(string forumID in this.terms.ForumIDs)
				{
					try
					{
		                this.queryString = this.queryString + " meta:Search.MSForums.ForumID(\"" + forumID + "\")";
						if (ordinal != this.terms.ForumIDs.Count)
						{
							this.queryString = this.queryString + " OR ";
						}
					}
					finally
					{
						ordinal++;
					}
				}
				this.queryString += ")";
			}

            if (this.terms.SiteID == "MSDN")
            {
                this.queryString = this.queryString + " meta:Search.MSForums.SiteID(\"1\")";
            }
            else if (this.terms.SiteID == "TechNet")
            {
                this.queryString = this.queryString + " meta:Search.MSForums.SiteID(\"17\")";
            }

			if (this.terms.IsQuestion)
			{
				this.queryString = this.queryString + " meta:Search.MSForums.IsQuestion(\"1\")";
				if (this.terms.IsAnswered == 1)
				{
					this.queryString = this.queryString + " meta:Search.MSForums.IsAnswered(\"1\")";
				}
				else if (this.terms.IsAnswered == 0)
				{
					this.queryString = this.queryString + " meta:Search.MSForums.IsAnswered(\"0\")";
				}
				if (this.terms.Helpful)
				{
					this.queryString = this.queryString + " meta:Search.MSForums.HelpfulThread(\"1\")";
				}
			}
			if (this.terms.IsComment)
			{
				this.queryString = this.queryString + " meta:Search.MSForums.IsQuestion(\"0\")";
			}
            return "http://search.msn.com/results.aspx?q=" + System.Web.HttpUtility.UrlEncode(this.queryString);
        }


        private string queryString;
		/// <revisionHistory>
		/// <revision author="Peter Ritchie" date="13-jul=06">
		/// removed CA1051 warning
		/// </revision>
		/// </revisionHistory>
		private SearchTerms terms;

		/// <revisionHistory>
		/// <revision author="Peter Ritchie" date="13-jul=06">
		/// Created
		/// </revision>
		/// </revisionHistory>
		public SearchTerms Terms
		{
			get { return terms; }
		}
    }
}

